package com.hongdee.crm.car.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.car.service.ITCrmCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/22.
 */
@RestController
@RequestMapping("/crm/car")
public class TCrmCarController {
    @Autowired
    private ITCrmCarService itCrmCarService;

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 分页查询导入
     * @author 徐钦勇
     */
    @RequestMapping("/impPage")
    @ResponseBody
    public Map impPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        Map map = this.itCrmCarService.impPage(start, limit, oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * @Name:车辆车主信息查询接口
     * @Date: 2017/8/16 15:50
     * @Author: Liu XiaoLong
     */
    @RequestMapping("/carAndOwner")
    public Map getCarAndOwner(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "start", defaultValue = "0") String start,
                              @RequestParam(name = "limit", defaultValue = "50") String limit,
                              @RequestBody Map data) throws Exception {
        String placeNumber = (String) data.get("licensePlateNamber");
        String maintainer = (String) data.get("maintainer");
        String maintainDate = (String) data.get("maintainDate");
        String dateStart = (String) data.get("dateStart");
        String dateStop = (String) data.get("dateStop");
        String companyId = (String) data.get("companyId");
        //String companyId = (String) data.get("companyId");
        Map map = this.itCrmCarService.getCarAndOwner(start, limit, placeNumber, maintainer, maintainDate, companyId, dateStart, dateStop, uid);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirm")
    @ResponseBody
    public ResultEntity impConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        this.itCrmCarService.impConfirm(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody List<Map<Object, String>> id) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        this.itCrmCarService.impConfirms(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        this.itCrmCarService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 术洪超
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itCrmCarService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 术洪超
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.itCrmCarService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 徐钦勇
     */
    @RequestMapping("/impDel")
    @ResponseBody
    public ResultEntity impDel(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        this.itCrmCarService.clearById(id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 单客户查询-查询
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "id", defaultValue = "") String id,
                    @RequestParam(name = "key", defaultValue = "") String key,
                    @RequestParam(name = "value", defaultValue = "") String value,
                    @RequestParam(name = "companyId", defaultValue = "") String companyId

    ) throws Exception {
        //value = value.trim();// 这个程式在运行。。
        Map map = this.itCrmCarService.page(start, limit,uid,companyId,id,key,value);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

    /**
     * @Name: 单车主多车辆查询
     * @Author: Liu XiaoLong
     * @Date: 2018/1/18 10:52
     * @Remark:
     */
    @RequestMapping("/pageList")
    public Map pageList (@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "start", defaultValue = "0") String start,
                         @RequestParam(name = "limit", defaultValue = "50") String limit,
                         @RequestParam(name = "key", defaultValue = "") String key,
                         @RequestParam(name = "value", defaultValue = "") String value,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId) throws  Exception {
     value = value.trim();
     Map map = this.itCrmCarService.pageList(start,limit,key,value,uid,companyId);
     map.put("success",true);
     map.put("msg","查询成功");
     return map;
    }

    /**
     * @param id       车主车辆信息
     * @param plateNum 车牌号
     * @return Map
     * @throws Exception
     * @desc 变更车牌号
     * @author 徐钦勇
     */
    @RequestMapping("/changePlate")
    @ResponseBody
    public ResultEntity changePlate(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "id", defaultValue = "") String id,
                                    @RequestParam(name = "plateNum", defaultValue = "") String plateNum) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarService.ChangePlate(id, plateNum);
        res.setSuccess(true);
        res.setMsg("修改成功！");
        return res;
    }

    /**
     * @param id 车主车辆信息
     * @param
     * @return Map
     * @throws Exception
     * @desc 送修人修改
     * @author 徐钦勇
     */
    @RequestMapping("/changeSender")
    @ResponseBody
    public ResultEntity changeSender(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id,
                                     @RequestParam(name = "name", defaultValue = "") String name,
                                     @RequestParam(name = "sex", defaultValue = "") String sex,
                                     @RequestParam(name = "phone", defaultValue = "") String phone,
                                     @RequestParam(name = "wxId", defaultValue = "") String wxId,
                                     @RequestBody Map map) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarService.ChangeSendor(id, name, sex, phone, map);
        res.setSuccess(true);
        res.setMsg("修改成功！");
        return res;
    }

    /**
     * 根据车辆ID 查询车辆信息
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/carInfo")
    @ResponseBody
    public Map carInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                       @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        String cid = TokenUtil.getCompanyId(uid);
        Map<String, Object> map = this.itCrmCarService.findById(plateNum, vin, cid);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

    /**
     * 根据车架VIN来获得送修人员电话
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/carPhone")
    @ResponseBody
    public ResultEntity carPhone(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();

        res.setMsg("查询成功！");
        res.setDataList(this.itCrmCarService.sPhone(vin, uid));
        res.setSuccess(true);
        return res;

    }


    /**
     * 查询客户标签
     */
    @RequestMapping("/carTable")
    @ResponseBody
    public List<Map> carTable(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        String cid = TokenUtil.getCompanyId(uid);
        List map = this.itCrmCarService.carTable();
        return map;
    }

    /***
     * 根据车辆查询所有车辆
     * @param uid
     * @param token
     * @param key
     * @param value
     * @return
     * @throws Exception
     */
    @RequestMapping("/appCar")
    @ResponseBody
    public Map appCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "token", defaultValue = "") String token,
                      @RequestParam(name = "key", defaultValue = "") String key,
                      @RequestParam(name = "start", defaultValue = "") String start,
                      @RequestParam(name = "limit", defaultValue = "") String limit,
                      @RequestParam(name = "value", defaultValue = "") String value) throws Exception {
        Map map = itCrmCarService.appCar(uid, key, value, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /***
     * 根据车辆ID 查询最近20条维系记录
     * @param uid
     * @param token
     * @param carId
     * @return
     * @throws Exception
     */
    @RequestMapping("/appConcat")
    @ResponseBody
    public ResultEntity carConcat(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "carId", defaultValue = "") String carId) throws Exception {
        if (StringUtils.isEmpty(carId)) {
            throw new Exception("请选择车辆");
        }
        ResultEntity res = new ResultEntity();
        res.setMsg("查询成功！");
        res.setDataList(itCrmCarService.carConcat(carId));
        res.setSuccess(true);
        return res;
    }


    @RequestMapping("/pushMsg")
    @ResponseBody
    public ResultEntity pushMsg(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "saId", defaultValue = "") String saId,
                                @RequestParam(name = "title", defaultValue = "") String title,
                                @RequestParam(name = "text", defaultValue = "") String text) throws Exception {
        String clientId = TokenUtil.getClientId(saId);
        itCrmCarService.push(clientId, title, text);
        ResultEntity re = new ResultEntity();
        re.setSuccess(true);
        re.setMsg("消息推送成功!!");
        return re;
    }


    /**
     * 任务计算器 生成任务 获得车辆详情
     *
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("taskCarInfo")
    @ResponseBody
    public ResultEntity taskCarInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "vin", defaultValue = "") String vin,
                                    @RequestParam(name = "taskType", defaultValue = "") String taskType) throws Exception {
        ResultEntity res = new ResultEntity();
        Map<String, Object> taskCarInfo = this.itCrmCarService.getTaskCarInfo(vin, TokenUtil.getCompanyId(uid), taskType);
        res.setData(taskCarInfo);
        res.setMsg("车辆信息查询成功");
        return res;
    }

    /**
     * 基盘车辆区域分布
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param map2
     * @return
     * @throws Exception
     */
    @RequestMapping("/baseCar")
    @ResponseBody
    public Map taskCarInfo(@RequestParam(name = "uid", defaultValue = "") String uid,

                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "start", defaultValue = "0") String start,
                           @RequestParam(name = "limit", defaultValue = "50") String limit,
                           @RequestBody Map map2) throws Exception {

        //String cid = TokenUtil.getCompanyId(uid);
        Map map = this.itCrmCarService.baseCar(uid, start, limit, map2);

        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

    /**
     * 基盘车辆区域分布详情
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/baseCarDetaill")
    @ResponseBody
    public Map taskCarInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "start", defaultValue = "0") String start,
                           @RequestParam(name = "limit", defaultValue = "50") String limit,
                           @RequestParam(name = "companyId", defaultValue = "") String companyId,
                           @RequestBody Map data) throws Exception {
        //String cid = TokenUtil.getCompanyId(uid);
        Map map = this.itCrmCarService.baseCarDetaill(uid, start, limit, companyId, data);

        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

}
